package com.ttl.bc.ap.purchaseorder.chemical;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;

import com.ttl.bc.ap.ap.bean.BCAPBean;
import com.ttl.bc.ap.purchaseorder.db.BCPurchaseOrderBO;
import com.ttl.bc.ap.stkrequest.BCStkRequestBO;
import com.ttl.bc.ic.stktransfer2.BCStkTransfer2BO;
import com.ttl.common.util.CommonUtils;



public class ChemicalPOAction extends LookupDispatchAction {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected Map getKeyMethodMap() {
		
		Map map = new HashMap();
		map.put("chemicalpoForm.add", "add");
		map.put("chemicalpoForm.update", "update");
		map.put("chemicalpoForm.delete", "delete");
		map.put("chemicalpoForm.blank", "blank");
		map.put("chemicalpoForm.cal", "cal");
		map.put("chemicalpoForm.createpo", "createpo");
		map.put("chemicalpoForm.ponew", "ponew");
		map.put("chemicalpoForm.chemicalHistory", "chemicalHistory");
		map.put("chemicalpoForm.showFind", "showFind");
		map.put("chemicalpoForm.view", "view");
		
		return map;
	}
	
	public ActionForward blank(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		String dept = request.getParameter("dept");

		request.setAttribute("mode", "find");
		request.setAttribute("dept", dept);
		return mapping.findForward("blank");		
	}
	
	public ActionForward cal(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String dept = request.getParameter("dept");
		
		List<CalStockItemBean> al = new ChemicalBO().cal(form, request, response);
		request.setAttribute("itemResults", al);
		
		request.setAttribute("mode", "add");
		request.setAttribute("dept", dept);		
		return mapping.findForward("blank");
	
	}		

	public ActionForward ponew(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String dept = request.getParameter("dept");
		
		String docNo = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-DC-");
		request.setAttribute("docNo", docNo);
		request.setAttribute("ittl_purchaseCode", docNo.substring(docNo.length()-6));
		request.setAttribute("mode", "add");
		request.setAttribute("dept", dept);		
		request.setAttribute("itemResults", null);
		request.setAttribute("ponew", "new");
		
		return mapping.findForward("createponew");
	}
	
	public ActionForward createpo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String dept = request.getParameter("dept");
		
		List<CalStockItemBean> al = new ChemicalBO().createpo(form, request, response);
		BCAPBean ap = new ChemicalBO().findAPofItem(al);
		request.setAttribute("ap", ap);
		request.setAttribute("itemResults", al);
		request.setAttribute("countId", al.size());
		
		request.setAttribute("ponew", "blank");
		
		String docNo = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-DC-");
		request.setAttribute("docNo", docNo);
		request.setAttribute("ittl_purchaseCode", docNo.substring(docNo.length()-6));
		request.setAttribute("mode", "add");
		request.setAttribute("dept", dept);		
		
		return mapping.findForward("createpo");
	}	
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
							
		if (new ChemicalBO().add(form, request, response)==true) {
			return mapping.findForward("success_add");
		}else {
			return mapping.findForward("fail_add");
		}
	}	

	public ActionForward chemicalHistory(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		List<ChemicalHistoryBean> al = new ChemicalBO().chemicalHistory(form, request, response);

		request.setAttribute("lineNumber", request.getParameter("lineNumber"));
		request.setAttribute("itemResults", al);
		request.setAttribute("itemCode", request.getParameter("itemCode"));
		
		List<CalStockItemBean> item = new ChemicalBO().calItem(form, request, response);
		request.setAttribute("item", item);
		
		return mapping.findForward("chemicalHistory");
	}
	
	public ActionForward showFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		if(new ChemicalBO().showFind(request)){
			return mapping.findForward("success_showFind");
		}else {
			return mapping.findForward("fail_showFind");
		}
	}
	
	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		if(new ChemicalBO().view(form, request, response)) {
			return mapping.findForward("success_view_chemicalpo");	
		}else {
			return mapping.findForward("failure_view_chemicalpo");
		}
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		if (new ChemicalBO().update(form, request, response)==true) {
			return mapping.findForward("success_update_chemicalpo");
		}else {
			return mapping.findForward("failure_update_chemicalpo");
		}		
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
					
		if (new ChemicalBO().delete(form, request, response)==true) {
						
			return mapping.findForward("success_delete_chemicalpo");
		}else {
			return mapping.findForward("failure_delete_chemicalpo");
		}		
	}	
}
